package com.xijia.wuyouwallet.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.utils.MyActivityManager;

import cn.pedant.SweetAlert.SweetAlertDialog;


/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class BaseActivity extends FragmentActivity {
    protected SweetAlertDialog progress;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        MyActivityManager.getInstance().addActivity(this);
    }


    //是否statusBar 状态栏为透明 的方法 默认为真
    protected boolean isTranslucentStatusBar() {
        return true;
    }

    /**
     * General method for starting a new activity either for result or not.
     *
     * @param activityClass The activity to start
     * @param bundle        Extra information with this intent.
     * @param isReturn      If start for result or not
     * @param requestCode   The request code.
     * @param isFinish      If finish self after start.
     */
    public void jumpToPage(Class<?> activityClass, Bundle bundle, boolean isReturn, int requestCode, boolean isFinish) {
        if (activityClass == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(this, activityClass);

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        if (isReturn) {
            startActivityForResult(intent, requestCode);
        } else {
            startActivity(intent);
        }

        if (isFinish) {
            finish();
        }
    }

    public void jumpToPage(Class<?> activityClass) {
        jumpToPage(activityClass, null, false, 0, false);
    }

    public void jumpToPage(Class<?> activityClass, boolean isFinish) {
        jumpToPage(activityClass, null, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> activityClass, Bundle bundle) {
        jumpToPage(activityClass, bundle, false, 0, false);
    }

    public void jumpToPage(Class<?> activityClass, Bundle bundle, boolean isFinish) {
        jumpToPage(activityClass, bundle, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> activityClass, boolean isReturn, int requestCode) {
        jumpToPage(activityClass, null, isReturn, requestCode, false);
    }

    /**
     * 弹出吐司
     *
     * @param msg
     */
    protected void showToast(final String msg) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 显示进度条
     */
    protected void showProgressDialog(final boolean isCancel) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (progress == null) {
                    progress = new SweetAlertDialog(BaseActivity.this, SweetAlertDialog.PROGRESS_TYPE);
                    progress.getProgressHelper().setBarColor(getResources().getColor(R.color.main_orange));
                    progress.setTitleText("加载中...");
                    if (isCancel){
                        progress.setCancelable(true);
                    }else {
                        progress.setCancelable(false);
                    }
                }
                progress.show();
            }
        });
    }

    /**
     * 隐藏进度条
     */
    protected void dismissProgressDialog() {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (progress != null) {
                    progress.dismiss();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        MyActivityManager.getInstance().exitNow(this);
        super.onDestroy();
    }

}
